package com.project.dao.Impl;

import com.project.bean.Employee;
import com.project.dao.IEmployeeDao;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestParam;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Repository
public class EmployeeDaoImpl extends BaseDao implements IEmployeeDao {
    @Override
    public List<Employee> findAll() {
        List<Employee> list=new ArrayList<>();
        this.setConnection();
        try {
            ps=con.prepareStatement("SELECT * FROM t_employee");
            rs= ps.executeQuery();
            while (rs.next()){
             list .add(this.getEmployee());
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {

        }
        return list;
    }

    private Employee getEmployee() {
        Employee employee = new Employee();
        try {
            employee.setId(rs.getInt("pk_enId"));
            employee.setName(rs.getString("e_name"));
            employee.setPhone(rs.getString("e_phone"));
            employee.setMoney(rs.getInt("e_money"));
            employee.setImgPath(rs.getString("e_faceImg"));
            employee.setStatus(rs.getString("e_status"));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return employee;
    }

    @Override
    public void add(Employee em) {
           this.setConnection();
        try {
            ps=con.prepareStatement("INSERT INTO t_employee(e_name,e_phone,e_money,e_faceImg,e_status)\n" +
                    "VALUES(?,?,?,?,?)");
            ps.setObject(1,em.getName());
            ps.setObject(2,em.getPhone());
            ps.setObject(3,em.getMoney());
            ps.setObject(4,em.getImgPath());
            ps.setObject(5,em.getStatus());
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
                this.closeConnection();
        }
    }

    @Override
    public void update(Integer id, Integer money) {
            this.setConnection();
        try {
            ps=con.prepareStatement("UPDATE t_employee SET e_money=? WHERE pk_enId=?");
            ps.setObject(1,money);
            ps.setObject(2,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public Employee findById(Integer id) {
        this.setConnection();
        try {
            ps=con.prepareStatement("SELECT * FROM t_employee WHERE pk_enId=?;");
            ps.setObject(1,id);
            rs= ps.executeQuery();
            while (rs.next()){
                return this.getEmployee();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
        return null;
    }

    @Override
    public void dimission(Integer id, String status) {
        this.setConnection();
        try {
            ps=con.prepareStatement("UPDATE t_employee SET e_status=? WHERE pk_enId=?");

            ps.setObject(1,status);
            ps.setObject(2,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            this.closeConnection();
        }
    }
}
